module.exports = function() {
  this._db = require("./db_CRUD")();
  this._db.init("mongodb://localhost:27017", "user", "info");
  this._userModel = {};
  this._isType = function(obj) {
    let type = Object.prototype.toString.call(obj);
    // console.log(type);
    switch (type) {
      case "[object Array]":
        type = "Array";
        break;
      case "[object String]":
        type = "String";
        break;
      case "[object Object]":
        type = "Object";
        break;
      case "[object Boolean]":
        type = "Boolean";
        break;
      case "[object Number]":
        type = "Number";
        break;
    }
    return type;
  };

  /**
   * 添加用户
   * addUser(user)
   *    user: 必须是一个JSON格式的object对象 如:{ "userName":'陈灿明', 'password':'272244064', age:'18', interest:[游戏, 电影, 小说, 漫画] }
   *
   */
  this._userModel.addUser = user => {
    return new Promise((resolve, reject) => {
      const type = this._isType(user);
      if (type == "Object") {
        this._db
          .add(user)
          .then(result => {
            resolve(result);
          })
          .catch(e => {
            reject(e);
          });
      } else {
        throw new Error(
          `addUser(${typeof user}) 参数必须是Object类型的JSON格式对象 如: addUser({'name':'陈灿明'}) `
        );
      }
    });
  };

  /**
   * 删除用户
   * deleteUser(userName)
   * userName: 必须是一个String类型
   * 如:deleteUser('陈灿明')
   */
  this._userModel.findUserNameDelete = nickname => {
    return new Promise((resolve, reject) => {
      const type = this._isType(nickname);
      if (type == "String") {
        this._db
          .delete({ 'userName': nickname })
          .then(result => {
            resolve(result);
          })
          .catch(e => {
            reject(e);
          });
      } else {
        throw new Error(
          `findUserNameDelete(${typeof nickname}) 参数必须是Object类型的JSON格式对象 如: findUserNameDelete('陈灿明')`
        );
      }
    });
  };

  /**
   * 查找昵称并更改
   * findUserNameUpdate(nickname, updateData)
   * nickname: 必须是一个String类型
   * updateData: 必须是一个Object类型
   * 如:findUserNameUpdate('陈灿明', {age:12})
   */
  this._userModel.findUserNameUpdate = (nickname, updateData) => {
    return new Promise((resolve, reject) => {
      const isnicknameType = this._isType(nickname);
      const isUpdateDataType = this._isType(updateData);
      if (isnicknameType == "String" && isUpdateDataType == "Object") {
        this._db
          .update({ 'userName': nickname }, updateData)
          .then(result => {
            resolve(result);
          })
          .catch(err => {
            reject(err);
          });
      } else {
        throw new Error(
          `参数错误 : findUserNameUpdate(${typeof nickname}, ${typeof updateData}) 应该是 findUserNameUpdate(string, object) `
        );
      }
    });
  };

    /**
   * 查找昵称
   * findUserName(nickname)
   * nickname: 必须是一个String类型
   * 如:findNicknameUpdate('陈灿明')
   * 
   * return 
   * 查找到返回1 查找不到返回0
   */
  this._userModel.findUserName = nickname => {
    return new Promise((resolve, reject) => {
      const isnicknameType = this._isType(nickname);
      if (isnicknameType == "String") {
        this._db
          .find({ 'userName': nickname })
          .then(result => {
            resolve(result);
          })
          .catch(err => {
            console.log(err);
            reject(err);
          });
      }else{
        throw new Error(
          `参数错误 : findUserName(${typeof nickname}) 应该是 findUserName(string) `
        );
      }
      
    });
  };

  return this._userModel;
};
